package com.quwan.app.controller;

import com.quwan.anno.RecordLog;
import com.quwan.anno.UpdateAccessTime;
import com.quwan.app.service.MemberDynamicService;
import com.quwan.app.vo.DynamicDetailsDTO;
import com.quwan.app.vo.MemberDynamicDTO;
import com.quwan.common.BaseController;

import com.quwan.common.enums.RecordLogApiEnum;
import com.quwan.common.result.ApiResult;
import com.quwan.common.result.PageResult;
import com.quwan.common.result.ResultCode;
import com.quwan.entity.TDynamicComment;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * @author quan
 * @date 2021-07-24 10:27
 */
@Api(tags = "会员动态")
@RestController
@RequestMapping("app/dynamic")
@RequiredArgsConstructor
public class DynamicController extends BaseController {
    private final MemberDynamicService dynamicService;

    @ApiOperation(value = "App会员动态列表", notes = "<span style=\"color:red\">注意参数是否必填,返回数据中" +
            "id——动态主键id;title——内容;follow——点赞数量;comment—评论数量;status——当前动态审核通过;topic——动态参与主题(目前没用); " +
            "item(动态图片或视频)[id——主键;dynamicId——动态主键id;type——0图片 1 视频;showColor——展示颜色;icon——图标]" +
            "</span>")
    @PostMapping("list")
    public ApiResult<PageResult<MemberDynamicDTO>> memberDynamicList(HttpServletRequest request,
                                                                     @RequestParam(defaultValue = "1") int pageNo,
                                                                     @RequestParam(defaultValue = "10") int pageSize,
                                                                     @ApiParam(value = "0:审核状态  1:正常") @RequestParam String status,
                                                                     @ApiParam(value = "0:自己 1:推荐 2:关注") @RequestParam(required = false) String type) {
        String mId = "";

        try {
            mId = getId(request);
        } catch (Exception e) {

        }

        return ApiResult.success(dynamicService.getMemberDynamicList(pageNo, pageSize, mId, status, type));

    }

    @ApiOperation("查看用户动态列表")
    @PostMapping("member/list")
    public ApiResult<PageResult<MemberDynamicDTO>> memberDynamicList(HttpServletRequest request,
                                                                     @RequestParam(defaultValue = "1") int pageNo,
                                                                     @RequestParam(defaultValue = "10") int pageSize,
                                                                     @ApiParam("目标会员") @RequestParam("id") String mId) {
        return ApiResult.success(dynamicService.getMemberDynamicListById(getId(request), pageNo, pageSize, mId));
    }

    @ApiOperation("点赞动态")
    @PostMapping("follow")
    public ApiResult followDynamic(HttpServletRequest request,
                                   @ApiParam("动态id") @RequestParam String dynamicId) {

        if (dynamicService.addDynamicFollow(getId(request), dynamicId))
            return ApiResult.success();
        else
            return ApiResult.error(ResultCode.FAIL);
    }

    @ApiOperation("取消点赞")
    @PostMapping("cancel")
    public ApiResult cancelFollowDynamic(HttpServletRequest request,
                                         @ApiParam("动态id") @RequestParam String dynamicId) {
        if (dynamicService.cancelDynamicFollow(getId(request), dynamicId))
            return ApiResult.success();
        else
            return ApiResult.error(ResultCode.FAIL);
    }

    @ApiOperation(value = "(评论)动态评论列表", notes = "<span style=\"color:red\">注意参数是否必填,返回数据中" +
            "id——动态评论主键id;dynamicId——动态主键id;message——评论内容;comment—评论数量;reply——是否回复;commentMember——评论用户;mid——评论用户id" +
            "</span>")
    @PostMapping("commentList")
    public ApiResult<PageResult<TDynamicComment>> commentList(HttpServletRequest request,
                                                              @RequestParam(defaultValue = "1") int pageNo,
                                                              @RequestParam(defaultValue = "10") int pageSize,
                                                              @ApiParam("动态id") @RequestParam String dynamicId) {
        return ApiResult.success(dynamicService.getDynamicList(pageNo, pageSize, dynamicId, getId(request)));
    }

    @RecordLog(RecordLogApiEnum.PUBLISH_DYNAMIC)
    @ApiOperation("发布动态")
    @PostMapping("publish")
    public ApiResult publishDynamic(HttpServletRequest request,
                                    @ApiParam("动态标题") @RequestParam String title,
                                    @ApiParam("参与主题") @RequestParam(required = false) String topic,
                                    @ApiParam("动态 图片或视频") @RequestParam(required = false) String files) {

        if (StringUtils.isBlank(title) && StringUtils.isBlank(files))
            return ApiResult.error(ResultCode.PARAM_MISS);
        boolean success = dynamicService.publishDynamic(getId(request), title, topic, files);
        if (success)
            return ApiResult.success();
        else
            return ApiResult.error(ResultCode.INTERNAL_SERVER_ERROR);
    }

    @ApiOperation("评论动态")
    @PostMapping("commentDynamic")
    public ApiResult commentDynamic(HttpServletRequest request,
                                    @ApiParam("评论内容") @RequestParam String message,
                                    @ApiParam("动态id") @RequestParam String dynamicId) {

        boolean success = dynamicService.addDynamicComment(dynamicId, getId(request), message);
        if (success)
            return ApiResult.success();
        else
            return ApiResult.error(ResultCode.INTERNAL_SERVER_ERROR);
    }

    @UpdateAccessTime
    @ApiOperation("动态详情")
    @PostMapping("detail")
    public ApiResult<DynamicDetailsDTO> dynamicDetails(HttpServletRequest request,
                                                       @ApiParam("动态id") @RequestParam("id") String dynamicId) {
        return ApiResult.success(dynamicService.getDynamicDetails(getId(request), dynamicId));
    }

    @ApiOperation("动态（评论点赞）")
    @PostMapping("cfollow")
    public ApiResult followComment(HttpServletRequest request,
                                   @ApiParam("评论Id") @RequestParam("id") String commentId,
                                   @ApiParam("0取消 1点赞") @RequestParam() Integer status) {
        dynamicService.followComment(getId(request), commentId, status);
        return ApiResult.success();
    }

    @ApiOperation("评论回复（仅限作者）")
    @PostMapping("comment/reply")
    public ApiResult commentReply(HttpServletRequest request,
                                  @ApiParam("评论Id") @RequestParam("id") String commentId,
                                  @ApiParam("评论内容") @RequestParam() String content) {
        dynamicService.commentReply(getId(request), commentId, content);
        return ApiResult.success();
    }

    @ApiOperation("删除动态")
    @PostMapping("del")
    public ApiResult delDynamic(HttpServletRequest request,
                                @ApiParam("动态id") @RequestParam("id") String id) {
        boolean success = dynamicService.delDynamic(getId(request), id);
        if (success)
            return ApiResult.success();
        return ApiResult.failed();
    }
}
